<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="bootstrap-3.3.7-dist/js/jquery-3.4.1.min.js"></script>
    <script src="bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <link rel="stylesheet" href="css/together.css">
</head>
<body>
<div class="container-fluid" >
    <!--导航栏开始-->
    <div>
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="index.html" style="margin-left: -50px;">清尘沐歌</a>
                </div>
                <div>
                    <ul class="nav navbar-nav">
                        <li class=""><a href="index.html"><span class="glyphicon glyphicon-home"></span> 首页</a></li>
                        <li><a href="community.html">社区</a></li>
                        <li>
                            <div class="input-group">
                                <form class="navbar-form navbar-left" role="search">
                                    <input type="text" class="form-control " placeholder="搜索..."/>
                                    <span class="input-group-btn">
                                    <button type="button" class="btn "><span class="glyphicon glyphicon-search"></span></button>
                                </span>
                                </form>
                            </div>
                        </li>
                    </ul>
                </div>
                <div>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#" data-toggle="modal" data-target="#register"><span class="glyphicon glyphicon-user" ></span> 注册</a></li>
                        <li><a href="#" data-toggle="modal" data-target="#login" style="margin-right: 0px;"><span class="glyphicon glyphicon-log-in" ></span> 登录</a></li>
                    </ul>
                </div>
            </div>
        </nav>
    </div>
    <!--导航栏结束-->
</div>



<!--注册开始-->
<!--模态框 -->
<div class="modal fade" id="register" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog " role="document">
        <div class="modal-content  col-sm-10">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="register1">用户注册</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="registerFrom" method="get" action="user_login.html">
                    <div class="form-group">
                        <label for="usernameObj" class="col-sm-2 control-label" style="padding-right: 0px;">用户名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="usernameObj1" placeholder="Username">
                        </div>
                        <span style="margin: 0px 0px 0px 93px;color: red;" id="usernameMsg1"></span>
                    </div>
                    <div class="form-group">
                        <label for="passwordObj" class="col-sm-2 control-label" style="padding-right: 0px;">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="passwordObj1" placeholder="Password">
                        </div>
                        <span style="margin: 0px 0px 0px 93px;color: red;" id="passwordMsg1"></span>
                    </div>
                    <div class="form-group">
                        <label for="passwordObj" class="col-sm-2 control-label" style="padding-right: 0px;">确认密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="passwordObj2" placeholder="Password">
                        </div>
                        <span style="margin: 0px 0px 0px 93px;color: red;" id="passwordMsg2"></span>
                    </div>



                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-8">
                            <button type="submit" class="btn btn-default" onclick="return register()" id="re">注册</button>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!--注册结束-->

<!--登录开始-->
<!--模态框 -->
<div class="modal fade" id="login" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog " role="document">
        <div class="modal-content  col-sm-10">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">用户登录</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="loginFrom" method="get" action="user_login.html">
                    <div class="form-group">
                        <label for="usernameObj" class="col-sm-2 control-label">用户名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="usernameObj" placeholder="Username">
                        </div>
                        <span style="margin: 0px 0px 0px 93px;color: red;" id="usernameMsg"></span>
                    </div>
                    <div class="form-group">
                        <label for="passwordObj" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="passwordObj" placeholder="Password">
                        </div>
                        <span style="margin: 0px 0px 0px 93px;color: red;" id="passwordMsg"></span>
                    </div>

                    <div class="form-group">
                        <label for="passwordObj" class="col-sm-2 control-label">验证码</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="text" placeholder="验证码">

                            <canvas style="margin: -52px 26px 12px 238px" id="canvas" width="120" height="40"></canvas>
                        </div>

                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-8">
                            <button type="submit" class="btn btn-default" onclick="return login()" id="bt">登录</button>
                            <button type="reset" class="btn btn-default">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--登录结束-->


<script>
    /*注册验证开始*/
    //jQuery
    var usernameObj1=document.getElementById("usernameObj1");//注册用户名
    var passwordObj1=document.getElementById("passwordObj1");//注册密码
    var passwordObj2=document.getElementById("passwordObj2");//确认密码


    /*注册验证结束*/

    /*登录验证开始*/
    //javaScript
    var canvas = document.getElementById("canvas");//演员
    var context = canvas.getContext("2d");//舞台，getContext() 方法可返回一个对象，该对象提供了用于在画布上绘图的方法和属性。
    var button = document.getElementById("bt");//获取按钮
    var input = document.getElementById("text");//获取输入框
    draw();
    canvas.onclick = function () {
        context.clearRect(0, 0, 120, 40);//在给定的矩形内清除指定的像素
        draw();
    }
    // 随机颜色函数
    function getColor() {
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }
    function draw() {
        context.strokeRect(0, 0, 120, 40);//绘制矩形（无填充）
        var aCode = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
        // 绘制字母
        var arr = [] //定义一个数组用来接收产生的随机数
        var num //定义容器接收验证码
        for (var i = 0; i < 4; i++) {
            var x = 20 + i * 20;//每个字母之间间隔20
            var y = 20 + 10 * Math.random();//y轴方向位置为20-30随机
            var index = Math.floor(Math.random() * aCode.length);//随机索引值
            var txt = aCode[index];
            context.font = "bold 20px 微软雅黑";//设置或返回文本内容的当前字体属性
            context.fillStyle=getColor();//设置或返回用于填充绘画的颜色、渐变或模式，随机
            context.translate(x,y);//重新映射画布上的 (0,0) 位置，字母不可以旋转移动，所以移动容器
            var deg=90*Math.random()*Math.PI/180;//0-90度随机旋转
            context.rotate(deg);// 	旋转当前绘图
            context.fillText(txt, 0, 0);//在画布上绘制“被填充的”文本
            context.rotate(-deg);//将画布旋转回初始状态
            context.translate(-x,-y);//将画布移动回初始状态
            arr[i] = txt //接收产生的随机数
        }
        num = arr[0] + arr[1] + arr[2] + arr[3] //将产生的验证码放入num
        // 绘制干扰线条
        for (var i = 0; i < 8; i++) {
            context.beginPath();//起始一条路径，或重置当前路径
            context.moveTo(Math.random() * 120, Math.random() * 40);//把路径移动到画布中的随机点，不创建线条
            context.lineTo(Math.random() * 120, Math.random() * 40);//添加一个新点，然后在画布中创建从该点到最后指定点的线条
            context.strokeStyle=getColor();//随机线条颜色
            context.stroke();// 	绘制已定义的路径
        }
        // 绘制干扰点，和上述步骤一样，此处用长度为1的线代替点
        for (var i = 0; i < 20; i++) {
            context.beginPath();
            var x = Math.random() * 120;
            var y = Math.random() * 40;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.strokeStyle=getColor();
            context.stroke();
        }

        //点击按钮验证
        button.onclick = function () {
            /*用户名密码验证开始*/
            //1.得到姓名和密码
            var usernameObj = document.getElementById("usernameObj").value;
            var passwordObj = document.getElementById("passwordObj").value;
            //判断用户名和密码是否为空
            if (usernameObj == null || usernameObj.trim() == "") {//去除前后空格
                document.getElementById("usernameMsg").innerHTML = "用户名不能为空";
                return false;
            } else if(!(/^[a-zA-Z_]\w{0,9}$/).test(usernameObj)) {
                usernameMsg.innerHTML = "字母数字下划线开始，长度1到10位, 不能是数字开头";
                return false;
            } else {
                document.getElementById("usernameMsg").innerHTML = "";
            }
            if (passwordObj == null || passwordObj.trim() == "") {//去除前后空格
                document.getElementById("passwordMsg").innerHTML = "密码不能为空";
                return false;
            }else if (!(/^.{6,12}$/).test(passwordObj)) {
                passwordMsg.innerHTML = "输入字符为6到12位";
                return false;
            } else {
                document.getElementById("passwordMsg").innerHTML = "";
                //return true;
            }
            /*用户名密码验证结束*/

            /*验证码开始*/
            var text = input.value //获取输入框的值
            if (text === num) {
                //以上验证通过，表单提交
                return true;
            } else {
                alert('验证码错误，验证失败')
                return false;
            }
            /*验证码结束*/
        }

    }
    /*登录验证结束*/



</script>
</body>
</html>